﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;

namespace CCTool.Scripts.XueXueGIS
{
  internal class Button_RepairName : Button
  {

    protected override void OnClick()
    {
      var mv = MapView.Active;
      var lyrs = mv.Map.Layers;
      var tables = mv.Map.StandaloneTables;

      var lyrList = mv.GetSelectedLayers();
      var tableList = mv.GetSelectedStandaloneTables();

      if (lyrList.Count() == 0 && tableList.Count()==0)
      {
        foreach (var lyr in lyrs)
        {
          string featureLayerName;
          if (lyr is FeatureLayer)
          {
            FeatureLayer featureLayer = lyr as FeatureLayer;
            //方法必须在QueuedTask.Run中运行
            QueuedTask.Run(() =>
            {

              try
              {
                featureLayerName = featureLayer.GetFeatureClass().GetDefinition().GetName();
              }
              catch
              {
                featureLayerName = featureLayer.GetFeatureClass().GetDefinition().GetAliasName();
              }

              if (featureLayerName == "")
              {
                featureLayerName = featureLayer.GetFeatureClass().GetDefinition().GetName();
              }
              lyr.SetName(featureLayerName);
            });
          }

          else if (lyr is RasterLayer)
          {
            RasterLayer rasterLayer = lyr as RasterLayer;
            QueuedTask.Run(() =>
            {
              string rasterLayerName = rasterLayer.GetRaster().GetRasterDataset().GetName();
              lyr.SetName(rasterLayerName);
            });
          }

          else
          {

          }
        }

        foreach (var table in tables)
        {
          //方法必须在QueuedTask.Run中运行
          QueuedTask.Run(() =>
          {
              string tableName = table.GetTable().GetDefinition().GetAliasName();
              table.SetName(tableName);
          });
        }
      }

      else
      {
        foreach (var lyr in lyrList)
        {
          string featureLayerName;
          if (lyr is FeatureLayer)
          {
            FeatureLayer featureLayer = lyr as FeatureLayer;
            //方法必须在QueuedTask.Run中运行
            QueuedTask.Run(() =>
            {
              
              try
              {
                featureLayerName = featureLayer.GetFeatureClass().GetDefinition().GetName();
              }
              catch
              {
                featureLayerName = featureLayer.GetFeatureClass().GetDefinition().GetAliasName();
              }
                            
              if (featureLayerName == "")
              {
                featureLayerName = featureLayer.GetFeatureClass().GetDefinition().GetName();
              }
              lyr.SetName(featureLayerName);
            });
          }

          else if (lyr is RasterLayer)
          {
            RasterLayer rasterLayer = lyr as RasterLayer;
            QueuedTask.Run(() =>
            {
              string rasterLayerName = rasterLayer.GetRaster().GetRasterDataset().GetName();
              lyr.SetName(rasterLayerName);
            });
          }

          else
          {

          }
        }
        foreach (var table in tableList)
        {
          //方法必须在QueuedTask.Run中运行
          QueuedTask.Run(() =>
          {
            string tableName = table.GetTable().GetDefinition().GetAliasName();
            if (tableName == "")
            {
              tableName = table.GetTable().GetDefinition().GetName();
            }
            table.SetName(tableName);
          });
        }
      }
    }
  }
}
